package cn.edu.neusoft.yuejian4.yuejian_4.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.BuildConfig;
import cn.edu.neusoft.yuejian4.yuejian_4.Config.Server;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Order;

/**
 * Created by Lix on 2018/10/12.
 */

public class OrderListAdapter  extends RecyclerView.Adapter<OrderListAdapter.ViewHolder> {

    private List<Order> mOrder;
    private Context mContext;
    mOnItemClickListener mOnItemClickListener;

    public OrderListAdapter(Context mContext, List<Order> mOrder){
        this.mContext = mContext;
        this.mOrder = mOrder;
        //mLayoutInflater = LayoutInflater.from(mContext);
    }
    @Override
    public OrderListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_cardview,parent,false);
        OrderListAdapter.ViewHolder holder = new OrderListAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(OrderListAdapter.ViewHolder holder, final int position) {

        final Order order = mOrder.get(position);
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener!=null) {
                    mOnItemClickListener.onClick(v, position);
                }
            }
        };
        holder.train_name.setText(order.getTrain_name());
        holder.order_time.setText(order.getPay_time());
        Picasso.with(mContext)
                .load(Server.PIC_URL_J+order.getPic())
                .placeholder(R.drawable.ic_default_img)
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .into(holder.train_img);
        if(order.getFlag()==1){
            holder.state.setText("已完成");
            holder.order_return_pay.setText("申请退款");
        }else if(order.getFlag()==2){
            holder.state.setText("待完成");
            holder.order_return_pay.setText("付款");
        }
        holder.order_return_pay.setOnClickListener(onClickListener);
        holder.order_del.setOnClickListener(onClickListener);

    }

    @Override
    public int getItemCount() {
        return mOrder.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView train_img;
        TextView train_name;
        TextView order_time,state;
        Button order_del;
        Button order_return_pay;
        FrameLayout order_container;

        public ViewHolder(View view){
            super(view);
            state = (TextView)view.findViewById(R.id.order_state);
            train_img = (ImageView)view.findViewById(R.id.order_train_img);
            train_name = (TextView)view.findViewById(R.id.order_train_name);
            order_time = (TextView)view.findViewById(R.id.order_paytime);
            order_del = (Button)view.findViewById(R.id.order_del);
            order_return_pay = (Button)view.findViewById(R.id.order_return_pay);
            order_container = (FrameLayout)view.findViewById(R.id.order_container);

        }
    }

    public interface mOnItemClickListener {
        void onClick(View view, int position);
    }

    public void setmOnItemClickListener(OrderListAdapter.mOnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
